#include <os/ipc.h>
#define NUM_MAX_CONDITION 16


condition_t condition_table[NUM_MAX_CONDITION]={{
	.inuse = NOTUSING,
	.key = 42356,
}};

int do_cond_init(int key){
	int i=0;
	for(i=0;i<NUM_MAX_CONDITION;i++){
		if(condition_table[i].inuse == NOTUSING)
			break;
	}
	condition_table[i].key=key;
	condition_table[i].num_waiting=0;
	condition_table[i].inuse=USING;
	queue_init(&(condition_table[i].block_queue));
	return i;
}
int do_cond_wait(int key,int lock_id){
	int i=0;
	for(i=0;i<NUM_MAX_CONDITION;i++)
		if(condition_table[i].key==key && condition_table[i].inuse== USING)
			break;
	if(i==NUM_MAX_CONDITION)
		return 0;
	condition_table[i].num_waiting++;
	do_binsemop(lock_id,1);//unlock
	do_block(&(condition_table[i].block_queue));
	do_binsemop(lock_id,0);
	return 1;
}
int do_cond_destroy(int key){
	int i=0;
	for(i=0;i<NUM_MAX_CONDITION;i++)
		if(condition_table[i].key==key && condition_table[i].inuse== USING)
			break;
	if(i==NUM_MAX_CONDITION)
		return 0;
	condition_table[i].inuse = NOTUSING;
	while(!queue_is_empty(&(condition_table[i].block_queue)))
		do_unblock(&(condition_table[i].block_queue));
	condition_table[i].num_waiting=0;
	return 1;
}
int do_cond_signal(int key){

}
int do_cond_broadcast(int key){
	int i=0;
	for(i=0;i<NUM_MAX_CONDITION;i++)
		if(condition_table[i].key==key && condition_table[i].inuse== USING)
			break;
	if(i==NUM_MAX_CONDITION)
		return 0;
	while(!queue_is_empty(&(condition_table[i].block_queue)))
		do_unblock(&(condition_table[i].block_queue));
	condition_table[i].num_waiting=0;
	return 1;
}
shmpage_t shared={
	.key=0,
	.refer_num = 0,
	.pa = 0x55001000
};
void* do_shmpageget(int key){
	shared.key=key;
	shared.refer_num++;
	uint64_t va = findupage(shared.pa);
	return (void *)va;
}
void do_shmpagedt(void *addr){
	freeupage((uint64_t)addr);
	shared.refer_num--;
	if (shared.refer_num <= 0)
		memset(shared.pa+0xffffffc000000000,0,1<<12);
	return;
}